#include <memory.h>
#include "net_file_decompress.h"

// 解压 GZIP 数据函数实现，返回解压后的数据长度
int decompress_gzip(const unsigned char* src, size_t src_len, unsigned char* dest, size_t dest_len, size_t* out_len)
{
    z_stream stream;
    memset(&stream, 0, sizeof(stream));

    // 初始化解压状态
    int ret = inflateInit2(&stream, 16 + MAX_WBITS);  // 16 + MAX_WBITS 用于支持 GZIP 格式
    if (ret != Z_OK) {
        return ret;
    }

    stream.avail_in = src_len;
    stream.next_in = (Bytef*)src;
    stream.avail_out = dest_len;
    stream.next_out = dest;

    // 解压
    ret = inflate(&stream, Z_FINISH);
    if (ret != Z_STREAM_END) {
        inflateEnd(&stream);
        return ret;
    }

    // 获取解压后的数据长度
    *out_len = stream.total_out;

    // 清理并结束解压
    inflateEnd(&stream);
    return Z_OK;
}
